<!---
	* Date:26/1/2010 09:49:26 
	* Author: Cristian Costantini 
	* Version: 1.0.0 beta 2
	* License: http://www.apache.org/licenses/LICENSE-2.0
	 --->
	
<cfcomponent output="false">
    
    <cffunction name="init" access="public" returntype="cacheManagement.factory.CacheFactory" output="false">
        <cfargument name="rootLib" type="String" required="true" />
		<cfargument name="loadLib" type="boolean" required="true" />
        <cfscript>
		var path = expandPath( arguments.rootLib );
        var paths = arrayNew(1);
		var libraries = getLibraries( path );
		
		for( i=1; i<=libraries.recordCount; i++ ){
		
			paths.add( path & libraries['name'][i] );
		
		}
		
		setLoader( createObject("component", "cacheManagement.io.javaloader.JavaLoader").init( paths ) );
		
		variables.loadLib = arguments.loadLib;
        </cfscript>
        <cfreturn this />
    </cffunction>
    
    <cffunction name="createCacheManager" access="public" returntype="Any" output="false" hint="Create a CacheManager object">
		<cfreturn createInstance("net.sf.ehcache.CacheManager") />
    </cffunction>
    
    <cffunction name="createCache" access="public" returntype="Any" output="false" hint="Create a Cache object">
        <cfreturn createInstance("net.sf.ehcache.Cache") />
    </cffunction>
    
    <cffunction name="createElement" access="public" returntype="Any" output="false" hint="Create an Element object">
        <cfreturn createInstance("net.sf.ehcache.Element") />
    </cffunction>
	
	<cffunction name="createCacheStatistics" access="public" returntype="Any" hint="Create a CacheStatistics object">
		<cfargument name="cache" type="Any" required="true" hint="Cache object" />
		<cfreturn createObject( 'component', 'cacheManagement.util.Statistics' ).init( arguments.cache ) />
	</cffunction>
	
	<cffunction name="createEventManager" access="public" returntype="Any" hint="Create an EventManager object">
		<cfreturn createObject( 'component', 'cacheManagement.event.EventManager' ).init() />
	</cffunction>
    
    <!--- private --->
	
	<cffunction name="createInstance" access="private" returntype="Any" output="false">
		<cfargument name="class" type="String" required="true" />
		<cfif variables.loadLib >
		
			<cfreturn getLoader().create( arguments.class ) />
		
		<cfelse>
		
			<cfreturn createObject( 'java', arguments.class ) />
		
		</cfif>
	</cffunction>
    
    <cffunction name="setLoader" access="private" returntype="void" output="false">
        <cfargument name="loader" type="cacheManagement.io.javaloader.JavaLoader" required="true" />
        <cfset variables.loader = arguments.loader />
    </cffunction>
    
    <cffunction name="getLoader" access="private" returntype="cacheManagement.io.javaloader.JavaLoader" output="false">
        <cfreturn variables.loader />
    </cffunction>
	
	<cffunction name="getLibraries" access="private" returntype="Query" output="false" >
		<cfargument name="rootLib" type="String" required="true" />
		<cfset var libs = 0 />
		<cfdirectory action="list" directory="#arguments.rootLib#" name="libs" filter="*.jar" />
		<cfreturn libs />
	</cffunction>
     
</cfcomponent>